﻿Imports System.Configuration
Imports System.Xml
Imports System.Reflection

Public Class ConfigHelper
    ''' <summary>
    ''' Returns the full path and (real) file name of the config (app.config)
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function GetConfigPath() As String
        Return Assembly.GetExecutingAssembly().Location + ".config"
    End Function

    ''' <summary>
    ''' Loads the configuration file app.config located in the exe folder.
    ''' </summary>
    ''' <returns>An XmlDocument representing the app.config</returns>
    ''' <remarks>For private (ConfigHelper) use only.</remarks>
    Private Shared Function LoadConfigDocument() As XmlDocument
        Dim doc As XmlDocument = Nothing

        Try
            'initialize the doc object
            doc = New XmlDocument()

            'load doc with file from assembly
            doc.Load(ConfigHelper.GetConfigPath())
        Catch ex As Exception
            'oops just throw an error so code above the stack can deal with it
            Throw New ApplicationException("No configuration file found!", ex)
        End Try

        Return doc
    End Function
    ''' <summary>
    ''' Returns the Initial Ini Location
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>Static/Shared method does not require object initialization. Use s = ConfigHelper.GetInitialLocation()</remarks>
    Public Shared Function GetInitialIniLocation()
        Return ConfigurationManager.AppSettings("InitialIniLocation")
    End Function

    ''' <summary>
    ''' Writes a setting to the app.config file. If the key does not exist, it is created.
    ''' </summary>
    ''' <param name="key"></param>
    ''' <param name="value"></param>
    ''' <remarks></remarks>
    Private Shared Sub WriteSetting(ByVal key As String, ByVal value As String)
        Dim doc As XmlDocument = LoadConfigDocument()

        Dim node As XmlNode = doc.SelectSingleNode("//appSettings")

        If (node Is Nothing) Then
            Throw New InvalidOperationException("appSettings section is not found in the config file.")
        Else
            Try
                Dim elem As XmlElement = node.SelectSingleNode(String.Format("//add[@key='{0}']", key))

                If Not elem Is Nothing Then
                    elem.SetAttribute("value", value)
                Else
                    elem = doc.CreateElement("add")
                    elem.SetAttribute("key", key)
                    elem.SetAttribute("value", value)
                    node.AppendChild(elem)
                End If

                doc.Save(GetConfigPath)
            Catch ex As Exception

            End Try
        End If
    End Sub

    ''' <summary>
    ''' Writes the ini file location to the app.config file
    ''' </summary>
    ''' <param name="value"></param>
    ''' <remarks></remarks>
    Public Shared Sub WriteInitialIniLocation(ByVal value As String)
        WriteSetting("InitialIniLocation", value)
    End Sub
End Class
